package com.cheyun.netsalev3.view.aftersale

import android.annotation.SuppressLint
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.bean.aftersale.CarOwnerData
import com.cheyun.netsalev3.bean.aftersale.MaintenanceDetail
import com.cheyun.netsalev3.databinding.ActivityEditMaintenanceBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.aftersale.EditMaintenanceActivityViewModel

class EditMaintenanceActivity : BaseActivity<ActivityEditMaintenanceBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_edit_maintenance
    }
    var firstEdit:Int = 0
    var data = MaintenanceDetail()
    override fun initViewModel(): ViewModel? {
        var param = intent.getSerializableExtra("data")

        return if (param == null) {
            //添加保养预约
            binding.headview.titleText?.text = "登记保养预约"

            var factory = InjectorUtils.getEditMaintenanceViewModelFactory(data)
            ViewModelProviders.of(this@EditMaintenanceActivity, factory).get(EditMaintenanceActivityViewModel::class.java)
        } else {
            //编辑保养预约
            data = param as MaintenanceDetail
            var factory = InjectorUtils.getEditMaintenanceViewModelFactory(data)
            firstEdit = 1
            ViewModelProviders.of(this@EditMaintenanceActivity, factory).get(EditMaintenanceActivityViewModel::class.java)
        }

        return null
    }

    @SuppressLint("ResourceAsColor")
    override fun initData() {
        binding.viewModel?.reservetime?.observe(this, Observer { it ->
            binding.txtReservetime.text = it
        })
        binding.viewModel?.sercivename?.observe(this, Observer { it ->
            binding.txtSercivename.text = it
        })
        if(data.reservetype.equals("1")){
            binding.txtUname.isEnabled = false
            binding.txtPhone.isEnabled = false
            binding.txtVin.isEnabled = false
            binding.txtEngineno.isEnabled = false
            binding.txtLicense.isEnabled = false
            binding.txtSeriesname.isEnabled = false
            binding.txtSpecname.isEnabled = false

            binding.txtUname.setTextColor(R.color.colorB)
            binding.txtPhone.setTextColor(R.color.colorB)
            binding.txtVin.setTextColor(R.color.colorB)
            binding.txtEngineno.setTextColor(R.color.colorB)
            binding.txtLicense.setTextColor(R.color.colorB)
            binding.txtSeriesname.setTextColor(R.color.colorB)
            binding.txtSpecname.setTextColor(R.color.colorB)

            binding.txtSearch1.visibility = View.GONE
            binding.txtSearch2.visibility = View.GONE
        }
    }


    override fun initView() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode==102){
            data?.apply {
                var carBrandParam = getSerializableExtra("car") as CarBrandParam
                binding.viewModel?.setCarData(carBrandParam)
                binding.txtSeriesname.text = carBrandParam.seriesname
                binding.txtSpecname.text = carBrandParam.specname
            }
        }else if(resultCode == 101){
            data?.apply {
                var data = data?.getSerializableExtra("data") as CarOwnerData
                var carBrandParam = CarBrandParam()
                carBrandParam.series = data.seriesid
                carBrandParam.seriesname = data.seriesname
                carBrandParam.spec = data.specid
                carBrandParam.specname = data.specname
                binding.txtSeriesname.text = carBrandParam.seriesname
                binding.txtSpecname.text = carBrandParam.specname

                binding.txtEngineno.setText(data.engineno)

                binding.viewModel?.setCarData(carBrandParam)

                binding.viewModel?.setCarOwner(data)
            }
        }
    }
}
